package com.andy.wan.android.mvp.ui.fragment;

import com.andy.ui.base.BasePageStateFragment;
import com.andy.ui.widget.PageStateView;
import com.andy.wan.android.R;
import com.andy.wan.android.mvp.api.WanAndroidApi;
import com.andy.wan.android.mvp.bean.BaseResponse;
import com.andy.wan.android.mvp.bean.ProjectClassifyData;
import com.andy.wan.android.mvp.net.RetrofitUtil;
import com.andy.wan.android.mvp.ui.adapter.ProjectFragmentPagerAdapter;
import com.google.android.material.tabs.TabLayout;

import java.util.List;

import androidx.viewpager.widget.ViewPager;
import butterknife.BindView;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * @Author: luofan
 * @CreateDate: 2020/3/17 10:35
 * @Description:
 */
public class ProjectViewpagerFragment extends BasePageStateFragment {

    @BindView(R.id.tab_layout)
    TabLayout mTabLayout;
    @BindView(R.id.view_pager)
    ViewPager mViewPager;
    @BindView(R.id.page_stat_view)
    PageStateView mPageStatView;

    private ProjectFragmentPagerAdapter mPagerAdapter;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_project_viewpager;
    }

    @Override
    public void initData() {

    }

    @Override
    public void initLogic() {
        mTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        if (getFragmentManager() != null) {
            mPagerAdapter = new ProjectFragmentPagerAdapter(getFragmentManager(), getContext());
            mViewPager.setOffscreenPageLimit(3);
            mViewPager.setAdapter(mPagerAdapter);
            requestData();
        }
    }

    private void requestData() {
        Observable<BaseResponse<List<ProjectClassifyData>>> observable = RetrofitUtil.getInstance().create(WanAndroidApi.class).getProjectClassifyData();
        requestData(observable, new Observer<BaseResponse<List<ProjectClassifyData>>>() {
            @Override
            public void onSubscribe(Disposable d) {
                getPageStateView().showViewByState(PageStateView.STATE_LOADING);
            }

            @Override
            public void onNext(BaseResponse<List<ProjectClassifyData>> listBaseResponse) {
                getPageStateView().showViewByState(PageStateView.STATE_CONTENT);
                mPagerAdapter.setDataList(listBaseResponse.getData());
                mTabLayout.setupWithViewPager(mViewPager);
            }

            @Override
            public void onError(Throwable e) {
                getPageStateView().showViewByState(PageStateView.STATE_ERROR);
            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    protected PageStateView getPageStateView() {
        return mPageStatView;
    }
}
